<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="分类名称">
        <el-select size="small" v-model="queryParams.siteClassId" placeholder="请选择分类名称" @change="handleFind">
          <el-option v-for="item in siteClassList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" icon="RefreshLeft" plain @click="handleBack" v-hasPermi="['webManage:navigation:back']">返回
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['webManage:navigation:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['webManage:navigation:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="linkList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection"/>
      <el-table-column label="网址logo" width="150" align="center">
        <template #default="scope">
          <el-avatar :src="scope.row.logo"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="网址标题" align="center">
        <template #default="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="网址地址" align="center">
        <template #default="scope">
          <span>{{ scope.row.url }}</span>
        </template>
      </el-table-column>
      <el-table-column label="网址描述" align="center">
        <template #default="scope">
          <span>{{ scope.row.info }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="250" align="center" prop="createTime">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="primary" @click="handleUpdate(scope.row) " v-hasPermi="['webManage:navigation:edit']">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row)" v-hasPermi="['webManage:navigation:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" center>
      <el-form :model="form" :rules="rules" ref="tagRef">
        <el-form-item prop="logo" label="网址头像logo">
          <el-upload
              name="file"
              class="avatar-uploader"
              :action="uploadImgUrl"
              :on-success="handleUploadSuccess"
              :on-remove="handleDeletePic"
              :show-file-list="false">
            <el-avatar v-if="form.logo" :size="50" :src="form.logo"/>
            <el-icon v-else class="avatar-uploader-icon ">
              <Plus/>
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="网址标题" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="网址地址" :label-width="formLabelWidth" prop="url">
          <el-input v-model="form.url" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="网址描述" :label-width="formLabelWidth" prop="info">
          <el-input v-model="form.info" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" style="margin-inline: 46%">确 定</el-button>
        <el-button @click="cancel" style="margin-inline: -45%">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Category">
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import {addCategory, delCategory, getCategory, listCategory, updateCategory} from "@/api/blog/category";
import SvgIcon from "@/components/SvgIcon/index.vue";
import IconSelect from "@/components/IconSelect/index.vue";
import {addLink, delLink, getLink, listLink, updateLink} from "@/api/blog/friendLink";
import {addClass, delClass, getClass, listClass, updateClass} from "@/api/blog/siteClass";
import {useRoute, useRouter} from "vue-router";
import {addNavigation, delNavigation, getNavigation, listNavigation, updateNavigation} from "@/api/blog/navigation";

const {proxy} = getCurrentInstance();

const showChooseIcon = ref(false);
const iconSelectRef = ref(null);
const linkList = ref([]);
const siteClassList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const names = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const formLabelWidth = '120px';
const uploadImgUrl = ref(import.meta.env.VITE_APP_BASE_API + "/common/upload");
const router = useRouter();
const route = useRoute();
let siteClassId = route.query.id;
const data = reactive({
  form: {},
  queryParams: {
    siteClassId: parseInt(siteClassId),
    title: null,
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    name: [
      {required: true, message: '标题不能为空', trigger: 'blur'},
    ],
    url: [
      {required: true, message: '地址不能为空', trigger: 'blur'},
    ],
    logo: [
      {required: true, message: 'logo不能为空', trigger: 'blur'},
    ]
  },
});

const {queryParams, form, rules} = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listNavigation(queryParams.value).then(response => {
    linkList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    siteClassId: queryParams.value.siteClassId,
    name: "",
    sort: ""
  };
  proxy.resetForm("tagRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  names.value = selection.map(item => item.name);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加网站类别";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getNavigation(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改网站类别";
  });
}

// 上传成功回调
function handleUploadSuccess(res, file) {
  if (res.code === 200) {
    form.value.logo = res.url;
  }
}

function handleDeletePic(res, file) {
  form.value.logo = null;
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["tagRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateNavigation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNavigation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value
  const name = row.name || names.value
  proxy.$modal.confirm('是否确认删除网址标题为"' + name + '"的数据项？').then(function () {
    return delNavigation(id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 展示下拉图标 */
function showSelectIcon() {
  iconSelectRef.value.reset();
  showChooseIcon.value = true;
}

/** 选择图标 */
function selected(name) {
  form.value.icon = name;
  showChooseIcon.value = false;
}

/** 图标外层点击隐藏下拉列表 */
function hideSelectIcon(event) {
  var elem = event.relatedTarget || event.srcElement || event.target || event.currentTarget;
  var className = elem.className;
  if (className !== "el-input__inner") {
    showChooseIcon.value = false;
  }
}

function onClick(url) {
  window.open(url)
}

function handleBack() {
  router.push({path: '/webManage/siteClass'})
}

function getSiteClassList() {
  let query1 = {
    pageNo: 1, pageSize: 100
  }
  listClass(query1).then(response => {
    siteClassList.value = response.rows;
  }).catch(err => {
    console.error(err)
  });
}

function handleFind() {
  console.log("打印" + queryParams.value.siteClassId)
  queryParams.value.pageNum = 1
  getList();
}

getList();
getSiteClassList();
</script>
